package com.hzhjhh.view.manageView.menus.storage.inReturnGoods;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.BackSale;
import com.hzhjhh.mapper.BackSaleMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.storage.InReturnGoodsDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-进/退货-提交按钮
 * 
 * @author 12083
 *
 */
public class SubmitBt {

	private static InReturnGoodsDialog inReturnGoodsDialog;
	private static BackSaleMapper backSaleMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(InReturnGoodsDialog inReturnGoodsDialog) {
		
		SubmitBt.inReturnGoodsDialog = inReturnGoodsDialog;
		JComboBox jComboBox1 = inReturnGoodsDialog.getJComboBox1();
		JButton submit = new JButton();
		submit.setBounds(375, 436, 81, 30);
        submit.setText("提交");
        submit.addActionListener(event -> {
        	if (jComboBox1.getSelectedItem().equals("  进  货 ")) {
                insertData("1");
            }
            if (jComboBox1.getSelectedItem().equals("  退  货 ")) {
                insertData("0");
            }
        });
        
        // 初始化mapper
        SqlSession sqlSession = DBUtils.getSqlSession(true);
        backSaleMapper = sqlSession.getMapper(BackSaleMapper.class);
        
		return submit;
	}
	public static void insertData(String NextBack) {

		Vector vector1 = inReturnGoodsDialog.getVector1();
		JComboBox jComboBox2 = inReturnGoodsDialog.getJComboBox2();
		JTextArea jTextArea1 = inReturnGoodsDialog.getJTextArea1();
		
        int insertnumber = 0;
        Date now = new Date();
        List<BackSale> backSaleList = new ArrayList<>();
        
        while (insertnumber < vector1.size()) {
            Vector insertVector = new Vector();
            insertVector = (Vector) vector1.get(insertnumber);
            String str1 = insertVector.get(0).toString(); //商品编号
            String str2 = insertVector.get(1).toString(); //商品名称
            String str3 = insertVector.get(2).toString(); //商品数量
            Double num1 = new Double(str3); //转数据类型
            String str4 = insertVector.get(3).toString(); //单价
            Double num2 = new Double(str4); //转数据类型
            String str5 = insertVector.get(4).toString(); //日期
            String str6 = jComboBox2.getSelectedItem().toString();
            String str7 = NextBack;
            String str8 = jTextArea1.getText();
            
            BackSale backSale = new BackSale();
            backSale.setBackProductId(str1);
            backSale.setBackProductName(str2);
            backSale.setBackNumber(Integer.parseInt(str3));
            backSale.setPrice(str4);
            backSale.setBackDate(DateUtil.parse(str5, DateFormatContants.YYYY_MM_DD_HH_MM_SS));
            backSale.setBackProductId(str7);
            backSale.setBackReason(str8);
            backSale.setBackPerson(str6);
            backSale.setBackPersonType("test");
            
            backSale.setCreatedBy(str6);
            backSale.setCreatedDate(now);
            backSale.setUpdatedBy(str6);
            backSale.setUpdatedDate(now);
            backSale.setIsDeleted(0);
            
            backSaleList.add(backSale);
            insertnumber++;
        }
        // 批量落库
        if (CollectionUtils.isNotEmpty(backSaleList)) {
			backSaleMapper.batchInsert(backSaleList);
		}
        JOptionPane.showMessageDialog(inReturnGoodsDialog, "数据录入成功", "提示",
              JOptionPane.INFORMATION_MESSAGE);
    }
}
